package com.hdax.gulimall.ware.listener;


import com.hdax.common.to.mq.StockLockTo;
import com.hdax.gulimall.ware.service.WareSkuService;
import com.hdax.gulimall.ware.vo.OrderTo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues ="stock.release.stock.queue")
public class StockReleaseListener {


    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 解锁库存,库存自己过期的处理
     * 工作单的id，工作单详情
     */
    @RabbitHandler
    public void handleStockLockRelease(StockLockTo stockLockTo, Message message, Channel channel) throws IOException {
       try {
           System.out.println("进入解库存！！！！！！！！！");
           wareSkuService.handleStockLockRelease(stockLockTo);
           channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
       }catch (Exception e){
           e.printStackTrace();
           //重新回消息丢列里面
           channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
       }
    }


    /**
     * 解锁库存:订单取消库存解锁
     * @param orderTo
     * @param message
     * @param channel
     */
    @RabbitHandler
    public void handleOrderCloseRelease(OrderTo orderTo,Message message,Channel channel) throws IOException {
        System.out.println("订单关闭准备解锁库存");
        try {
            wareSkuService.handleStockLockRelease(orderTo);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
